import { Article } from "../../models/article";
import { UserInfo } from "../../models/userinfo";
import { Factory } from "../../models/factory";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    productList: [],
    dataList: [],
    isLoading: false,
    count: 0,
    pageNum: 1,
    pageSize: 10,
    haveMore: true,
    loadingType: "",
    userInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = options.id;
    let cid = options.cid;
    this.data.id = id;
    this.data.cid = cid;
    let userInfo = new UserInfo();
    this.setData({
      userInfo: userInfo.getUserInfo()
    });
    this.init();
  },
  canSee(Articlelassify_id) {
    if (
      this.data.userInfo.popedomId != "1290559635618533376" &&
      Articlelassify_id == 2
    ) {
      return false;
    } else {
      return true;
    }
  },
  canNotSee(Articlelassify_id) {
    if (
      this.data.userInfo.popedomId != "1290559635618533376" &&
      Articlelassify_id == 2
    ) {
      return true;
    } else {
      return false;
    }
  },
  // 根据userId查工厂Id
  async getFactoryIdByUserId(userId) {
    let postData = {
      Search: { all: true, userId: userId, keyword: null },
      PageIndex: 1,
      PageRows: 10000,
      SortField: "id",
      SortType: "asc"
    };
    const data = await Factory.GetDataList(postData);
    if (data.Data.length > 0) {
      return data.Data[0].Id;
    }
  },
  async goDetail(e) {
    // console.log(e.currentTarget.dataset.item);
    // if (this.canNotSee(e.currentTarget.dataset.aid)) {
    //   return;
    // }
    let item = JSON.stringify(e.currentTarget.dataset.item);
    // wx.navigateTo({
    //   url: `/pages/new-product-detail/new-product-detail?id=${e.currentTarget.dataset.id}&item=${item}`
    // });
    console.log("userId",e.currentTarget.dataset.item.User_id );

    let factoryId = await this.getFactoryIdByUserId(
      e.currentTarget.dataset.item.User_id
    );
    console.log("factoryId",factoryId);
    wx.navigateTo({
      url: `/pages/manufacturer-detail/manufacturer-detail?id=${factoryId}&item=${item}`
    });
    // this.$router.push({
    //   path: "new-product-detail",
    //   query: {
    //     id,

    //     item: JSON.stringify(item)
    //   }
    // });
  },
  async init() {
    this.setData({
      loadingType: "loading"
    });
    let postData =
      // {
      //   "PageIndex": 1,
      //   "PageRows": 10,
      //   "SortField": "id",
      //   "SortType": "asc"
      // }
      {
        Search: {
          all: false,
          Condition: "Subclassfyid",
          keyword: this.data.id
        },
        PageIndex: this.data.pageNum,
        PageRows: this.data.pageSize,
        SortField: "CreateTime",
        SortType: "desc"
      };
    // {
    //   Search: {
    //     all: true,
    //     userId: "",
    //     keyword: null,
    //     ClassifyId: this.data.cid
    //   },
    //   PageIndex: this.data.pageNum,
    //   PageRows: this.data.pageSize,
    //   SortField: "id",
    //   SortType: "desc"
    // };
    const data = await Article.GetDataList(postData);
    if (data.Data.length == 0) {
      this.setData({
        loadingType: "end"
      });
      return;
    }
    if (data.Data.length < 10) {
      this.setData({
        loadingType: "end"
      });
    }
    this.data.pageNum++;
    data.Data.map(item => {
      item.src = item.Imags.split(",")[0];
      item.srcs = item.Imags.split(",");
      item.time = item.CreateTime.split(".")[0];
      return item;
    });
    this.setData({
      productList: this.data.productList.concat(data.Data)
    });
    console.log(this.data.productList);
    // this.productList = data.Data
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.init();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
